//
//  OMHTTPRequest.m
//  RequestTest
//
//  Created by xntech on 13-8-19.
//  Copyright (c) 2013年 xntech. All rights reserved.
//

#import "OMHTTPRequest.h"
#import "OMHTTPConstant.h"

@implementation OMHTTPRequest
@synthesize method = _method;
@synthesize url = _url;
@synthesize timeoutSeconds;
@synthesize showErrorHub = _showErrorHub;
@synthesize showFailedHub = _showFailedHub;
@synthesize showLoadingHub = _showLoadingHub;
@synthesize body = _body;
@synthesize cancelBlock,startBlock,finishBlock,failedBlock,headerBlock;
- (id)initWithOMURL:(OMURL *)url
{
    self = [super init];
    if (self)
    {
        _userInfo = [[NSMutableDictionary alloc] init];
        self.url = url;
        
        self.timeoutSeconds = TIMEOUT_SECONDS;
        self.showLoadingHub = YES;
        self.showFailedHub = YES;
        self.showErrorHub = YES;
    }
    return self;
}
- (void)setUserInfoValue:(id)value forKey:(id)key
{
    [_userInfo setObject:value forKey:key];
}
- (id)userInfoValueForKey:(id)key
{
    return [_userInfo objectForKey:key];
}
- (void)startAsynchronous
{
    [[self getIntervalRequest] startAsynchronous];
}
- (void)startSynchronous
{
    [[self getIntervalRequest] startSynchronous];
}
- (void)startAsynchronous:(float)startInterval
{
    [self performSelector:@selector(startAsynchronous) withObject:nil afterDelay:startInterval];
}
- (void)startSynchronous:(float)startInterval
{
    [self performSelector:@selector(startSynchronous) withObject:nil afterDelay:startInterval];
}

- (ASIHTTPRequest *)getIntervalRequest
{
    if (_internalRequest != nil)
    {
        [self cancel];
        [_internalRequest release];
    }
    if(self.method == nil)
    {
        if (self.body == nil)
        {
            self.method = METHOD_GET;
        }
        else
        {
            self.method = METHOD_POST;
        }
    }
    if([self.method isEqualToString:METHOD_DELETE] || [self.method isEqualToString:METHOD_GET])
    {
        _internalRequest = [[ASIHTTPRequest alloc] initWithURL:[self.url URL]];
    }
    else if ([self.method isEqualToString:METHOD_PUT] || [self.method isEqualToString:METHOD_POST]){
        _internalRequest = [[ASIFormDataRequest alloc] initWithURL:[self.url URL]];
        [self addPostValue];
    }
    [_internalRequest setDelegate:self];
    [_internalRequest setTimeOutSeconds:self.timeoutSeconds];
    return _internalRequest;
}
- (NSString *)responseString
{
    return _internalRequest.responseString;
}
- (NSData *)responseData
{
    return _internalRequest.responseData;
}
- (NSDictionary *)responseHeader
{
    return _internalRequest.responseHeaders;
}
- (int)responseCode
{
    return _internalRequest.responseStatusCode;
}
- (NSString *)responseMessage
{
    return _internalRequest.responseStatusMessage;
}
- (NSArray *)responseCookies
{
    return _internalRequest.responseCookies;
}
- (BOOL)isExecuting
{
    return _internalRequest.isExecuting;
}
- (BOOL)isFinished
{
    return _internalRequest.isFinished;
}
- (BOOL)isCancelled
{
    return _internalRequest.isCancelled;
}

- (void)addPostValue
{
    ASIFormDataRequest  *request = (ASIFormDataRequest *)_internalRequest;
    for(NSString *key in [self.body allKeys])
    {
        id value = [self.body paramForyKey:key];
        if ([value isKindOfClass:[OMRequestParam class]])
        {
            OMRequestParam *paramValue = (OMRequestParam *)value;
            if ([paramValue isValid])
            {
                if ([paramValue isFileNotData])
                {
                    [request addFile:paramValue.filePath withFileName:paramValue.fileName andContentType:paramValue.contentType forKey:paramValue.key];
                }else
                {
                    [request addData:paramValue.data withFileName:paramValue.fileName andContentType:paramValue.contentType forKey:paramValue.key];
                }
            }
        }else
        {
            [request setPostValue:value forKey:key];
        }
    }
}
- (void)cancel
{
    if (_internalRequest.isExecuting)
    {
        [_internalRequest clearDelegatesAndCancel];
        if (self.cancelBlock)
        {
            self.cancelBlock(self);
        }
    }
}
- (void)dealloc
{
    if ([_internalRequest isExecuting])
    {
        [_internalRequest clearDelegatesAndCancel];
    }
    [_internalRequest release];
    [_userInfo release];
    [_url release];
    [_method release];
    [_body release];
    [super dealloc];
}
@end




@interface OMHTTPRequest(Reqeust)<ASIHTTPRequestDelegate>


@end
@implementation OMHTTPRequest(Reqeust)
- (void)requestStarted:(ASIHTTPRequest *)request
{
    if (_showLoadingHub)
    {
        
    }
    
    if (self.startBlock) {
        self.startBlock(self);
    }
}
- (void)request:(ASIHTTPRequest *)request didReceiveResponseHeaders:(NSDictionary *)responseHeaders
{
    if (headerBlock)
    {
        self.headerBlock(self,responseHeaders);
    }
}
- (void)requestFinished:(ASIHTTPRequest *)request
{
    if (self.finishBlock)
    {
        self.finishBlock(self);
    }
}
- (void)requestFailed:(ASIHTTPRequest *)request
{
    if (_showFailedHub)
    {
        
    }
    
    if (self.failedBlock) {
        self.failedBlock(self);
    }
}
@end